$(function () {
    $('#channel').click(function() {
        $('#select-channel').removeClass('hidden');
        $('#input-sales').addClass('hidden');
    });
    $('#sales').click(function() {
        $('#input-sales').removeClass('hidden');
        $('#select-channel').addClass('hidden');
    })
    $('#main-form-container').inputValidate({
        submit: {
            query: '#requestSubmit'
        },
        inputs: [{
            query: '#hotel',
            regexp: window.commonRegexp.hotelName,
            message: lang('public.name'),
            emptyMessage: lang('public.nameNull')
        }, {
            query: '#address',
            regexp: window.commonRegexp.cityName,
            message: lang('public.address'),
            emptyMessage: lang('book.addressNull')
        }, {
            query: '#mobile',
            regexp: window.commonRegexp.mobile,
            message: lang('tip.mobile'),
            emptyMessage: lang('tip.mobileNull')
        }, {
            query: '#requestVerifiedCode',
            regexp: window.commonRegexp.imageCode,
            message: lang('tip.imageCode'),
            emptyMessage: lang('tip.imageCodeNull')
        }, {
            query: '#name',
            regexp: window.commonRegexp.name,
            message: lang('dealer.lineman'),
            emptyMessage: lang('public.linemanNull')
        }]
    });
    $('#requestSubmit').on('successSubmit.amos.inputValidate', function (e) {
        var submitBtn = $(this);
        // 该处可放ajax请求
        submitBtn.attr('disabled', true);
        var params = {};
        params.hotel = $('#hotel').val();
        params.address = $('#address').val();
        params.mobile = $('#mobile').val();
        params.name = $('#name').val();
        params.verify = $('#requestVerifiedCode').val();
        params.universalTime = $('#addRequestUniversalTime').val();
        params.currencyId = $('#addRequestMultiCurrency').val();
        params.countryId = $('#addRequestUniversalTime option:selected').data('countryid');
        params.areaCode = $('.areaCodeValue').text().trim();
        params.cityId   = $('#cityContainer option:selected').data('cityid');
        params.districtId = $('#districtContainer option:selected').data('districtid');
        
        if($('#channel').prop('checked') && $("#select-channel").val() !=='0') {
            params.from =  $("#select-channel").find("option:selected").text();
        }
        if($('#sales').prop('checked') && $.trim($('#input-sales').val()) !== '') {
            params.from =  $('#input-sales').val();
        }
        if(!params.hasOwnProperty('from')) {
            $('#apply-tip').text(lang('public.source'));
            $('#apply-tip').parent().addClass('has-error');
            $('#requestSubmit').removeAttr('disabled');
            return false;
        }
        ajaxRequest('/Home/Public/apply', params, 'post', function (data) {
            //formInlineTip($('#requestSubmit'), lang('public.link'), 2);
            window.setTimeout(function () {
                window.location.href = '/';
            }, 1500);
        }, null, submitBtn);
    });
    var getProvinceList = function(universalTimeId){
        var params = {};
        params.universalTimeId = universalTimeId;
        ajaxRequest('/Home/PublicPage/getProvinceList', params, 'post', function (data) {
            var provinceHtml = '';
            $.each(data.data, function(key, value) {
                provinceHtml += '<option data-provinceId="'+value.provinceId+'">'+value.provincName+'</option>';
            });  
            $('#provinceContainer').html(provinceHtml);
            getCityList($("#provinceContainer").find('option:selected').data('provinceid'));
        }, function(error){
        });        
    };
    var getCityList = function(provinceId){
        var params = {};
        params.provinceId = provinceId;
        ajaxRequest('/Home/PublicPage/getCityList', params, 'post', function (data) {
            var cityHtml = '';
            $.each(data.data, function(key, value) {
                cityHtml += '<option data-cityid="'+value.cityId+'">'+value.cityName+'</option>';
            });  
            $('#cityContainer').html(cityHtml);
            getDistrict($("#cityContainer").find('option:selected').data('cityid'));
        }, function(error){
        });
    };
    var getDistrict = function(cityId){
        var params = {};
        params.city = cityId;
        ajaxRequest('/Home/Position/getDistrict', params, 'post', function (data) {
            var districtHtml = '';
            $.each(data.data, function(key, value) {
                districtHtml += '<option data-districtid="'+value.id+'">'+value.name+'</option>';
            });  
            $('#districtContainer').html(districtHtml);
        }, function(error){
        });
    };    
    getProvinceList($("#addRequestUniversalTime").val());
    
    //获取省份
    $("#addRequestUniversalTime").change(function(){
        var universalTimeId =parseInt($(this).val());
        getProvinceList(universalTimeId);
    });    
    //获取城市
    $("#provinceContainer").change(function(){
        var provinceId =parseInt($(this).find('option:selected').data('provinceid'));
        getCityList(provinceId);
    });    
    //获取区域
    $("#cityContainer").change(function(){
        var cityId =parseInt($(this).find('option:selected').data('cityid'));
        getDistrict(cityId);
    });    
    

});
